package com.lmbx.shzh.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.sql.*;
import java.util.List;
@Component
public class yuntu {

    public static void main(String[] args) throws Exception {
        JSONObject json = new JSONObject();
        json.put("type","301");
        JSONObject jsonResult = HttpClientUtils.httpPost("http://123.59.199.32:8880/app-wth/api/wth/image/wthimg-list",json );
        System.out.println("获取数据"+jsonResult);
        if (!jsonResult.get("code").equals(200)){
            System.out.println("获取数据失败");
            return;
        }

        ParamsDto paramsDto = JSON.parseObject(jsonResult.toJSONString(),ParamsDto.class);
        List<Params> result = paramsDto.getResult();

        /*---------------------从接口中获取数据成功，将获取到的数据导入数据库表中-----------------------------*/
        try {

            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");//加载数据库驱动
            System.out.println("加载数据库驱动成功");
            String url="jdbc:sqlserver://110.249.217.146:60821;";//声明数据库test的url
            String user="warn";//数据库的用户名
            String password="shyj-2020";//数据库的密码
            Params params = new Params();
            Statement pstm = null;
            ResultSet rs = null;
            String sql="insert into SHYJ_DP.dbo.yuntu (types,kind,tm,relpath)"+" values(?,?,?,?)";
            Connection conn=DriverManager.getConnection(url, user, password);

            PreparedStatement stmt =conn.prepareStatement(sql);//创建一个Statement对象
            for (int i = 0;i<result.size();i++){

                stmt.setString(1,""+result.get(i).getType());
                stmt.setString(2,""+result.get(i).getKind());
                stmt.setString(3,""+result.get(i).getTm());
                stmt.setString(4,"http://123.59.199.32:8880/Weather"+result.get(i).getRelpath());
                int count = stmt.executeUpdate();
                System.out.println("关闭数据库成功"+count);
            }

            conn.close();
            System.out.println("关闭数据库成功");
        }catch (ClassNotFoundException e) {
            e.printStackTrace();
        }catch (SQLException e) {
            e.printStackTrace();
        }
        result.forEach( params -> System.out.println("http://123.59.199.32:8880/Weather"+result.get(1).getRelpath()));
    }

    @Scheduled(cron = "0 0/5 * * * ?")
    public void saveDatas() {
        JSONObject json = new JSONObject();
        json.put("type","301");
        JSONObject jsonResult = HttpClientUtils.httpPost("http://123.59.199.32:8880/app-wth/api/wth/image/wthimg-list",json );
        System.out.println("获取数据失败"+jsonResult);
        if (!jsonResult.get("code").equals(200)){
            System.out.println("获取数据失败");
            return;
        }

        ParamsDto paramsDto = JSON.parseObject(jsonResult.toJSONString(),ParamsDto.class);
        List<Params> result = paramsDto.getResult();

        /*---------------------从接口中获取数据成功，将获取到的数据导入数据库表中-----------------------------*/
        try {

            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");//加载数据库驱动
            System.out.println("加载数据库驱动成功");
            String url="jdbc:sqlserver://110.249.217.146:60821;";//声明数据库test的url
            String user="warn";//数据库的用户名
            String password="shyj-2020";//数据库的密码
            Params params = new Params();
            Statement pstm = null;
            ResultSet rs = null;
            String sql="insert into SHYJ_DP.dbo.yuntu (types,kind,tm,relpath)"+" values(?,?,?,?)";
            Connection conn=DriverManager.getConnection(url, user, password);

            PreparedStatement stmt =conn.prepareStatement(sql);//创建一个Statement对象
            for (int i = 0;i<result.size();i++){

                stmt.setString(1,""+result.get(i).getType());
                stmt.setString(2,""+result.get(i).getKind());
                stmt.setString(3,""+result.get(i).getTm());
                stmt.setString(4,"http://123.59.199.32:8880/Weather"+result.get(i).getRelpath());
                int count = stmt.executeUpdate();
            }
            conn.close();
            System.out.println("关闭数据库成功");
        }catch (ClassNotFoundException e) {
            e.printStackTrace();
        }catch (SQLException e) {
            e.printStackTrace();
        }
        result.forEach( params -> System.out.println(""+result.get(1).getTm()));
    }

}
